package com.example.demo.service.impl;

import com.example.demo.entity.RegisterEntity;
import com.example.demo.dao.RegisterDao;

import com.example.demo.result.Result;
import com.example.demo.result.ResultUtil;
import com.example.demo.service.RegisterService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterDao registerDao;

    //根据日期查询
    @Override
    public Result getRegisterData(RegisterEntity registerEntity) {

        //根据姓名查询
        if(!registerEntity.getName().equals("null")){
            return ResultUtil.success(registerDao.selectByName(registerEntity.getName()));
        }
//        根据给定编号区间查询登记数据
        if (!registerEntity.getStartNumber().equals("null") &&!registerEntity.getStartDate().equals("null")){
//            int limitFirst = (registerEntity.getPageIndex()-1)*registerEntity.getPageSize();
//            int limitEnd = registerEntity.getPageIndex()*registerEntity.getPageSize();
            registerEntity.setLimitFirst((registerEntity.getPageIndex()-1)*registerEntity.getPageSize());
            registerEntity.setLimitEnd(registerEntity.getPageIndex()*registerEntity.getPageSize());
           return ResultUtil.getMsgSuccess(registerDao.seniorCountSelect(registerEntity),registerDao.seniorSelect(registerEntity));
        }
        if(!registerEntity.getStartNumber().equals("null")){
//            int first = registerDao.getIdBySerNumber(registerEntity.getSerialNumber());
//            int end = registerDao.getIdBySerNumber(registerEntity.getEndNumber());

            int limitFirst = (registerEntity.getPageIndex()-1)*registerEntity.getPageSize();
            int limitEnd = registerEntity.getPageIndex()*registerEntity.getPageSize();

            return ResultUtil.getMsgSuccess(registerDao.selectCountBySerialNumberRnage(registerEntity.getStartNumber(),registerEntity.getEndNumber()),registerDao.selectBySerialNumRange(registerEntity.getStartNumber(),registerEntity.getEndNumber() ,limitFirst,limitEnd));
        }
        //根据给定日期区间查询登记数据
        if (!registerEntity.getStartDate().equals("null")){
//            if(registerDao.selectByDate(registerEntity.getStartDate()) == null){
//                return ResultUtil.error("当前起始日期暂无登记数据");
//            }
//             if(registerDao.selectByDate(registerEntity.getEndDate()) == null){
//                return ResultUtil.error("当前截止日期暂无登记数据");
//            }
//                int first = registerDao.selectTheFirst(registerEntity.getStartDate());
//                int end = registerDao.selectTheEnd(registerEntity.getEndDate());
                int limitF = (registerEntity.getPageIndex()-1)*registerEntity.getPageSize();
                int limitE = registerEntity.getPageIndex()*registerEntity.getPageSize();

                return ResultUtil.getMsgSuccess(registerDao.selectCountByDateRange(registerEntity.getStartDate(),registerEntity.getEndDate()),registerDao.selectByDateRange(registerEntity.getStartDate(),registerEntity.getEndDate(),limitF,limitE));
        }
        registerEntity.setLimitFirst((registerEntity.getPageIndex()-1)*registerEntity.getPageSize());
        registerEntity.setLimitEnd((registerEntity.getPageIndex())*registerEntity.getPageSize());

        if(registerDao.getRegisterData(registerEntity) !=  null){

           return ResultUtil.getMsgSuccess(registerDao.selectCount(),registerDao.getRegisterData(registerEntity));
        }else {
            return ResultUtil.error("当前日期登记数为0");
        }
    }

    //登记
    @Override
    public Result registerData( RegisterEntity registerEntity) {
        if(registerDao.selectNumber(registerEntity.getSerialNumber()) == null){
            if (registerDao.registerData(registerEntity)){
                return ResultUtil.success("登记成功");
            }else {
                return ResultUtil.error("登记失败");
            }
        }else {
            return ResultUtil.exists();
        }
    }
    //根据姓名查询登记信息

    //插入图片路径
    @Override
    public Result insertUrl( String url , String serialNumber) {
        if (registerDao.insertUrl(url , serialNumber)){
            return ResultUtil.success();
        }else {
            return ResultUtil.error("修改失败");
        }
    }
    //获取行业或工种
    @Override
    public Result selectIndOrWorkT(String item) {
        if (item.equals("industry")) {
            return ResultUtil.success(registerDao.selectIndustry());
        }
        if (item.equals("workType")) {
            return ResultUtil.success(registerDao.selectWorkType());
        }
        return ResultUtil.error("请输入正确的信息");
    }
    //获取未制证人员信息
    @Override
    public Result getWhetherCertify(int pageIndex,int pageSize) {
        int limitFirst = (pageIndex-1)*pageSize;
        int limitEnd = pageIndex*pageSize;
        return ResultUtil.getMsgSuccess(registerDao.selectWithoutCertifyCount(),registerDao.getWhetherCertify(limitFirst,limitEnd));
    }
    //插入测试数据
    @Override
    public Result testInsert(RegisterEntity registerEntity) {
         String s = registerEntity.getSerialNumber();
         int a = Integer.parseInt(s);
        System.out.println(a);
         int b = 0;
        for (int i = 0; i < 100 ; i++) {
             b = a ++;
             String s1 = ""+ b;
            registerEntity.setSerialNumber(s1);
            registerDao.registerData(registerEntity);
        }

        return null;
    }

    //按要求获取已制证人员信息
    @Override
    public Result getCertified(RegisterEntity registerEntity) {
        int limitFirst = (registerEntity.getPageIndex() - 1) * registerEntity.getPageSize();
        int limitEnd = registerEntity.getPageIndex() * registerEntity.getPageSize();

        if(!registerEntity.getName().equals("null")){
            return ResultUtil.success(registerDao.selectCertifyByName(registerEntity.getName()));
        }
        //根据指定编号区间查询已制证人员信息
        if (!registerEntity.getStartNumber().equals("null")&&!registerEntity.getStartDate().equals("null")) {
            registerEntity.setLimitFirst(limitFirst);
            registerEntity.setLimitEnd(limitEnd);
            return ResultUtil.getMsgSuccess(registerDao.seniorCountSelectCertify(registerEntity),registerDao.seniorSelectCertify(registerEntity));
        }else if(!registerEntity.getStartNumber().equals("null")){
//            int firstId = registerDao.selectIdByNumber(registerEntity.getStartNumber());
//            int endId = registerDao.getIdBySerNumber(registerEntity.getEndNumber());
            System.out.println(1);
            return ResultUtil.getMsgSuccess(registerDao.selectCertificateCountBySerilNum(registerEntity.getStartNumber(),registerEntity.getEndNumber()),
                    registerDao.selectCertificateBySerialNum(registerEntity.getStartNumber(),registerEntity.getEndNumber(),limitFirst,limitEnd));
        }else if (!registerEntity.getStartDate().equals("null")){
            System.out.println(2);
            return ResultUtil.getMsgSuccess(registerDao.selectCertificateCountByDate(registerEntity.getStartDate(),registerEntity.getEndDate()),
                    registerDao.selectCertificateByDate(registerEntity.getStartDate(),registerEntity.getEndDate(),limitFirst,limitEnd));
        }else { System.out.println(3);
            return ResultUtil.getMsgSuccess(registerDao.getCertifiedCount(), registerDao.getCertified(limitFirst, limitEnd));}
        //根据指定日期区间查询已制证人员信息
//        if (!registerEntity.getStartDate().equals("null")){
////            if(registerDao.selectDataByDate(registerEntity.getStartDate()) == null){
////                return ResultUtil.error("当前起始日期暂无登记数据");
////            }
////            if(registerDao.selectDataByDate(registerEntity.getEndDate()) == null){
////                return ResultUtil.error("当前截止日期暂无登记数据");
////            }
////            String firstNum = registerDao.selectFirstNumberByDate(registerEntity.getStartDate());
////            String endNum = registerDao.selectEndNumberByDate(registerEntity.getEndDate());
////            int firstId = registerDao.selectIdByNumber(firstNum);
////            int endId = registerDao.selectIdByNumber(endNum);
//            System.out.println(2);
//            return ResultUtil.getMsgSuccess(registerDao.selectCertificateCountByDate(registerEntity.getStartDate(),registerEntity.getEndDate()),
//                    registerDao.selectCertificateByDate(registerEntity.getStartDate(),registerEntity.getEndDate(),limitFirst,limitEnd));
//        }
//            System.out.println(3);
//            return ResultUtil.getMsgSuccess(registerDao.getCertifiedCount(), registerDao.getCertified(limitFirst, limitEnd));
    }
    //根据姓名查询
    @Override
    public Result selectByName(String name){
        return ResultUtil.success(registerDao.selectByName(name));
    }

    @Override
    public Result updateRegisterMsg(RegisterEntity registerEntity) {
        if (registerDao.updateRegisterMsg(registerEntity)){
            return ResultUtil.success("修改成功");
        }
        ;return ResultUtil.error("修改失败");
    }


//    @Override
//    public Result test() {
//        return ResultUtil.success(registerDao.getRegisterData());
//    }
}
